个人知识库 个人知识库
首页
关于
  • C语言
  • CPlusPlus
  • Linux
  • PHP
  • Nginx
  • MySQL
  • Redis
  • Docker
  • Kubernetes
  • SRS
阅读
常用工具
  • 分类
  • 标签
  • 归档
GitHub

Agnes001

坚持是一件很伟大的事业
首页
关于
  • C语言
  • CPlusPlus
  • Linux
  • PHP
  • Nginx
  • MySQL
  • Redis
  • Docker
  • Kubernetes
  • SRS
阅读
常用工具
  • 分类
  • 标签
  • 归档
GitHub
  • C语言

  • CPlusPlus

    • 基础特性

      • 枚举
      • 字符指针
    • vs2019设置
    • C++11特性

    • 并发编程

    • 引用
    • 类和对象
    • 友元和运算符重载
    • 继承
    • 继承和多态
    • 模板
    • C++基础总结
    • 类型转换
    • 异常
    • 容器
    • 算法
    • C++程序设计
    • C++ Primer总结
    • 编程技巧
    • 标准库体系结构与内核分析
    • 设计模式
    • cmake配置C++工程
    • libcurl的使用总结
    • web开发框架--drogon
    • log4cplus使用
    • C++数据类型
    • 函数
    • 线程
    • 进程
    • 文件操作
    • 日常问题记录
    • Cpp案例程序
    • 多线程
    • 侯捷c++11新特性
      • 特性1:Variadic Templates 可变参数模板
      • 特性2:空指针nullptr
      • 特性3:initializer list
      • 特性4:explict
      • 特性5:=default, =delete
      • 特性6:alias template(模板的别名)
      • 特性7:type alias
      • 特性8:noexcept
      • 特性9:override final
      • 特性10:decltype
      • 特性11:Lambda
      • 特性12:Revalue references
      • 特性13:hashtable
    • 侯捷stl
  • Lua技术栈

  • edoyun

  • 内存管理

  • 数据结构

  • 网络编程

  • Linux

  • 池化技术

  • 操作系统

  • python

  • 编程技术
  • CPlusPlus
Agnes001
2022-03-19

侯捷c++11新特性

# 特性1:Variadic Templates 可变参数模板

// ...参数个数可变,参数类型可变
// sizeof...(args) 返回函数参数的数目
// sizeof...(Types) 返回类型参数的数目
// 可以很方便完成recursive function call
void print(){}//个数为0时调用,相当于终止点
template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args)
{
    cout << firstArg << endl;
    print(args...);
}
//与上面的函数可以并存,上面的是特化版本
template<typename... Types>
void print(const Types&... args){}

// 可以完成recursive inheritance
template<typename... Valuse> class tuple;
template<> class tuple<>{};
template<typename Head, typename... Tail>
class tuple<Head, Tail...>:private tuple<Tail...>{};

// recursive composition
template<typename... Values> class tup;
template<> class tup<>{};
template<typename Head, typename... Tail>
class tup<Head, Tail...>
{
  typedef tup<Tail...> composited;
protected:
  composited m_tail;
  Head m_head;
public:

}

# 特性2:空指针nullptr

对指针初始化为空时,赋值为nullptr

# 特性3:initializer list

int j{};//0
int* p{};//nullptr
int i{5.3};//warning:initialer list not allow narrowing
void print(std::initializer_list<int> vals)
{
  for(auto p=vals.begin(); p!=vals.end(); ++p){
    std::cout<<*p<<"\n";
  }
}

# 特性4:explict

explicit 用于转换convert的时候,明确的,不能转换

# 特性5:=default, =delete

Foo(const Foo&) copy ctor cannot be overload
//当自定义了拷贝构造函数,默认的拷贝构造函数无法=default或=delete
Foo& operator(const Foo&) operator= cannot be overload(copy assign)
//当自定义了拷贝赋值函数,默认的拷贝赋值函数无法=default或=delete

# 特性6:alias template(模板的别名)

template<typename T>
using Vec = std::vector<T, MyAlloc<T>>;

Vec<int> is equivalent to std::vector<int, MyAlloc<int>>

template template parameter

template<typename name, template<class> class Container>
class XCls{};
//Error: vector need two template parameter
XCls<MyString, vector> cl;
//以下版本是对的
template<typename T>
using Vec = vector<T, alloctor<T>>;
template<typename name, template<class> class Container>
class XCls{};
XCls<MyString, Vec> cl;

# 特性7:type alias

//typedef void(*func)(int, int);
using func = void(*)(int, int);

# 特性8:noexcept

//用来指明某个函数无法——或不打算——抛出异常。
void foo() noexcept;
//若有异常未在foo()内被处理,亦即如果foo()抛出异常,程序会被终止。

# 特性9:override final

virtual void func(int);//A
virtual void func(int) override;//B:A 告诉编译器B重写了A中的func函数

class A final{};//A不允许被继承
class A{
  vitural void f() final;//f函数不允许被重写
}

# 特性10:decltype

let the compiler find out the type of an expression

map<string,float> coll;
decltype(coll)::value_type elem;

应用1:声明返回类型

template<typename T1, typename T2>
decltype(x+y) add(T1 x, T2 y);//编译不通过

template<typename T1, typename T2>
auto add(T1 x, T2 y) -> decltype(x+y);//this uses the same syntax as for lambdas to declare return types.

应用2:超编程 metaprogramming

typedef typename decltype(obj)::iterator iType;

应用3:传递一个lambda类型 used to pass the type of a lambda

auto cmp = [](const Person& p1, const Person& p2){
  return p1.lastname()<p2.lastname() || 
    (p1.lastname()==p2.lastname() && p1.firstname()<p2.firstname());
};
std::set<Person,decltype(cmp)> coll(cmp);

# 特性11:Lambda

[]() mutable -> retType{};
[=] pass by value
[&] pass by reference
mutable, operator()被定义为non-const成员函数

# 特性12:Revalue references

solve the problem of unneccessary copying and enable perfect forwarding when the right-hand side of an assignment is an rvalue, then the left-hand side object can steal resources from the right-hand side object rather than performing a separate allocation Lvalue:可以出现在operate=左侧 Rvalue:只能出现在operate=右侧

# 特性13:hashtable

当元素个数大于bucket vector大小时,会rehashing,增长到原来bucket vector的二倍

编辑此页
#new feature
多线程
侯捷stl

← 多线程 侯捷stl →

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式